package com.swak.paxos.statemachine;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.swak.paxos.Value;
import com.swak.utils.Lists;

public class StateMachineFac implements StateMachine {

	private Logger logger = LoggerFactory.getLogger(StateMachine.class);

	List<StateMachine> sms;

	public StateMachineFac(List<StateMachine> sms) {
		this.sms = Lists.newArrayList(sms);
	}

	public StateMachineFac addStateMachine(StateMachine sm) {
		this.sms.add(sm);
		return this;
	}

	@Override
	public int getSmID() {
		return FacSmID;
	}

	@Override
	public boolean execute(int groupIdx, long instanceID, Value value) {
		try {
			int smID = value.getSmID();
			for (StateMachine sm : sms) {
				if (sm.getSmID() == smID) {
					return sm.execute(groupIdx, instanceID, value);
				}
			}
			return true;
		} catch (Exception e) {
			logger.error("Execute StateMachine Error.", e);
		}
		return false;
	}
}
